import { Component, OnInit } from '@angular/core';
import { UserManageService } from './user-manage.service';
import { checkArray } from '@core/utils/utils';
import { Page } from '@models/page';
import { BUSINESS_ENUM, RET_MSG_ENUM } from '@models';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-user-manage',
  templateUrl: './user-manage.component.html',
})
export class UserManageComponent implements OnInit {

  formModel: any = {};
  loading = false;
  userSexList = BUSINESS_ENUM.SEXList;
  page = new Page();

  dataSet: Array<any> = [];

  constructor(
    private message: NzMessageService,
    private service: UserManageService
  ) { }

  ngOnInit() {
    this.reset();
  }

  search() {
    this.refreshData(true);
  }

  refreshData(flag = false) {
    if (flag) {
      this.page.curPage = 1;
    }
    const params = {
      userNo: this.formModel.userNo || '',
      userName: this.formModel.userName || '',
      userSex: this.formModel.userSex || '',
      phone: this.formModel.phone || '',
      curPage: this.page.curPage,
      pageSize: this.page.pageSize,
    };
    this.loading = true;
    this.service.getUserListByPage(params).subscribe((_res: any) => {
      this.loading = false;
      this.dataSet = checkArray(_res.retList);
      this.page.totalRow = _res.totalRow;
      if (!this.dataSet.length) {
        this.message.success(RET_MSG_ENUM.SuccessButEmpty);
      }
    }, () => {
      this.loading = false;
    })
  }

  reset() {
    this.formModel = {};
    this.refreshData(true);
  }
}
